#include<bits/stdc++.h>
#define ONLINE_JUDGE
#define ll long long
#define ull unsigned long long
using namespace std;

const ll N = 110, INF = 0x3f3f3f3f;

ll n, q;
ll a[N][N][N];

void add(ll k, ll i, ll j, ll x)
{
    a[k][i][j] += x;
    a[k][i][j] += a[k - 1][i][j];
    a[k][i][j] += a[k][i - 1][j];
    a[k][i][j] += a[k][i][j - 1];

    a[k][i][j] -= a[k - 1][i - 1][j];
    a[k][i][j] -= a[k - 1][i][j - 1];
    a[k][i][j] -= a[k][i - 1][j - 1];

    a[k][i][j] += a[k - 1][i - 1][j - 1];
}

ll ask(ll lx, ll rx, ll ly, ll ry, ll lz, ll rz)
{
    ll sum = a[rx][ry][rz];
    sum -= a[lx - 1][ry][rz];
    sum -= a[rx][ly - 1][rz];
    sum -= a[rx][ry][lz - 1];

    sum += a[lx - 1][ly - 1][rz];
    sum += a[lx - 1][ry][lz - 1];
    sum += a[rx][ly - 1][lz - 1];

    sum -= a[lx - 1][ly - 1][lz - 1];
    return sum;
}

void solve()
{
    cin >> n;
    for (ll k = 1;k <= n;k++)
        for (ll i = 1;i <= n;i++)
            for (ll j = 1;j <= n;j++) {
                ll x;cin >> x;
                add(k, i, j, x);
            }

    cin >> q;
    while (q--) {
        ll lx, rx, ly, ry, lz, rz;
        cin >> lx >> rx >> ly >> ry >> lz >> rz;
        cout << ask(lx, rx, ly, ry, lz, rz) << '\n';
    }
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    ll T = 1;
    //cin>>T;
    for (ll i = 1;i <= T;i++) {
        solve();
    }
}